package com.integralPerformance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integralPerformance.common.Result;
import com.integralPerformance.mapper.ApplicationManagerMapper;
import com.integralPerformance.pojo.entity.ApplicationManager;
import com.integralPerformance.pojo.entity.JobPosition;
import com.integralPerformance.pojo.form.add.AddJobPosition;
import com.integralPerformance.pojo.query.JobPositionQuery;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.pojo.vo.JobPositionVo;
import com.integralPerformance.service.JobPositionService;
import com.integralPerformance.mapper.JobPositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @author admin
* @description 针对表【job_position(职称类型管理表)】的数据库操作Service实现
* @createDate 2024-04-03 21:42:22
*/
@Service
public class JobPositionServiceImpl extends ServiceImpl<JobPositionMapper, JobPosition>
    implements JobPositionService{

    @Autowired
    JobPositionMapper jobPositionMapper;

    @Autowired
    ApplicationManagerMapper applicationManagerMapper;

    @Override
    public Result<BaseVo<JobPositionVo>> getList(JobPositionQuery jobPositionQuery) {

        Page<JobPositionVo> page = new Page<>(jobPositionQuery.getCurrentPage(), jobPositionQuery.getPageSize());
        Page<JobPositionVo> result = jobPositionMapper.getList(page, jobPositionQuery);

        BaseVo<JobPositionVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(jobPositionQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(jobPositionQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);

    }

    @Override
    public Result<?> getType() {
       List<String> list= jobPositionMapper.getType();
        return Result.success(list);
    }

    @Override
    public Result<?> getDetails(String type) {
        List<Map<String,String>> list = jobPositionMapper.getDetails(type);
        return Result.success(list);
    }

    @Transactional
    @Override
    public Result deleteById(String id) {
        QueryWrapper<ApplicationManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_position_id",id);
        boolean exists = applicationManagerMapper.exists(queryWrapper);
        if(exists){
            return Result.fail("存在申请的管理内容，不允许删除");
        }else {
            jobPositionMapper.deleteById(id);
            return Result.success("删除成功");
        }
    }

    @Override
    public Result<?> addOne(AddJobPosition addJobPosition) {

        QueryWrapper<JobPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_name",addJobPosition.getTypeName())
                    .eq("level_name",addJobPosition.getLevelName())
                    .eq("corresponding_name",addJobPosition.getCorrespondingName());
        boolean exists = jobPositionMapper.exists(queryWrapper);
        if (exists){
            return Result.fail("已存在，请勿重复添加");
        }

        JobPosition jobPosition = new JobPosition();
        BeanUtil.copyProperties(addJobPosition,jobPosition);
        jobPositionMapper.insert(jobPosition);
        return Result.success("添加成功");
    }

}




